﻿using UnityEngine;
using System.Collections;

namespace ZWFrameWork
{
    public class Encrypt
    {
        public const string ValidateKey = "5ED6CCF4-F9F2-4C9F-B36D-0C2BADA0859F";

        public static byte[] g_SendByteMap =
        {
            0xD0, 0x42, 0x97, 0x07, 0x1A, 0x68, 0xD2, 0x27, 0x47, 0x13, 0xE4, 0xD1, 0x09, 0xB6, 0x8A, 0x75,
            0x82, 0x61, 0xB3, 0x55, 0xE9, 0x48, 0x02, 0x6D, 0x00, 0xEC, 0x9D, 0x51, 0xD8, 0x4F, 0x35, 0x7E,
            0xA7, 0x78, 0xCB, 0x01, 0xD9, 0x7A, 0x9B, 0x1D, 0x99, 0xFF, 0x9E, 0x12, 0xE3, 0xBF, 0x74, 0x22,
            0x98, 0x91, 0x1C, 0x92, 0x37, 0x33, 0x30, 0x14, 0x8D, 0xD6, 0xBE, 0x93, 0x24, 0xEA, 0x0A, 0x2A,
            0xD5, 0x67, 0xEB, 0x46, 0xCE, 0xDA, 0xDD, 0x4B, 0x6B, 0xD3, 0xAE, 0x70, 0x8E, 0xFD, 0x29, 0xCD,
            0x89, 0xC1, 0x3A, 0xC5, 0x18, 0xC9, 0xF7, 0x26, 0xFA, 0xEF, 0xF5, 0x1F, 0x79, 0x16, 0xB2, 0x83,
            0x6A, 0x0B, 0x80, 0x5C, 0xF3, 0x04, 0x66, 0x9C, 0xE7, 0x20, 0x3C, 0xFB, 0x59, 0x23, 0x63, 0x8F,
            0xC3, 0xA2, 0xE2, 0x9A, 0xBD, 0x08, 0xA6, 0x0F, 0x7C, 0x06, 0x05, 0x39, 0xF2, 0xC0, 0x6F, 0xBC,
            0x95, 0xA0, 0x86, 0x31, 0x94, 0xDE, 0xC4, 0xFE, 0x57, 0x4E, 0xDF, 0x64, 0xF8, 0x6E, 0x76, 0x3D,
            0xE6, 0x6C, 0xAC, 0xC2, 0xC6, 0x3F, 0x03, 0xB7, 0x3E, 0xB1, 0xF6, 0x56, 0xC8, 0xDC, 0xF9, 0x90,
            0xE5, 0x72, 0x1E, 0x38, 0x50, 0x2B, 0x17, 0x84, 0x7B, 0x28, 0xFC, 0x77, 0xE8, 0x4A, 0xA3, 0xA9,
            0xD4, 0xA4, 0xDB, 0xBA, 0x65, 0x0C, 0x49, 0x44, 0xA5, 0x7D, 0x0E, 0x40, 0xAD, 0x1B, 0x5E, 0x5D,
            0x53, 0xF1, 0x52, 0x3B, 0x58, 0xAB, 0x85, 0x11, 0x8B, 0xBB, 0x45, 0xCA, 0xB9, 0x43, 0xB0, 0xEE,
            0x32, 0xAF, 0x5A, 0xE0, 0x5B, 0x9F, 0x73, 0x34, 0x88, 0x5F, 0x19, 0xCF, 0x96, 0x36, 0x2D, 0x15,
            0x54, 0x69, 0x60, 0x2F, 0xF0, 0x10, 0xD7, 0xA1, 0xCC, 0x71, 0x7F, 0x0D, 0xB8, 0x2E, 0x41, 0xC7,
            0xAA, 0x21, 0x87, 0x8C, 0x4D, 0xA8, 0xE1, 0xF4, 0x81, 0xB5, 0x62, 0xB4, 0x4C, 0x2C, 0x25, 0xED
        };

        public static byte[] g_RecvByteMap =
        {
            0x18, 0x23, 0x16, 0x96, 0x65, 0x7A, 0x79, 0x03, 0x75, 0x0C, 0x3E, 0x61, 0xB5, 0xEB, 0xBA, 0x77,
            0xE5, 0xC7, 0x2B, 0x09, 0x37, 0xDF, 0x5D, 0xA6, 0x54, 0xDA, 0x04, 0xBD, 0x32, 0x27, 0xA2, 0x5B,
            0x69, 0xF1, 0x2F, 0x6D, 0x3C, 0xFE, 0x57, 0x07, 0xA9, 0x4E, 0x3F, 0xA5, 0xFD, 0xDE, 0xED, 0xE3,
            0x36, 0x83, 0xD0, 0x35, 0xD7, 0x1E, 0xDD, 0x34, 0xA3, 0x7B, 0x52, 0xC3, 0x6A, 0x8F, 0x98, 0x95,
            0xBB, 0xEE, 0x01, 0xCD, 0xB7, 0xCA, 0x43, 0x08, 0x15, 0xB6, 0xAD, 0x47, 0xFC, 0xF4, 0x89, 0x1D,
            0xA4, 0x1B, 0xC2, 0xC0, 0xE0, 0x13, 0x9B, 0x88, 0xC4, 0x6C, 0xD2, 0xD4, 0x63, 0xBF, 0xBE, 0xD9,
            0xE2, 0x11, 0xFA, 0x6E, 0x8B, 0xB4, 0x66, 0x41, 0x05, 0xE1, 0x60, 0x48, 0x91, 0x17, 0x8D, 0x7E,
            0x4B, 0xE9, 0xA1, 0xD6, 0x2E, 0x0F, 0x8E, 0xAB, 0x21, 0x5C, 0x25, 0xA8, 0x78, 0xB9, 0x1F, 0xEA,
            0x62, 0xF8, 0x10, 0x5F, 0xA7, 0xC6, 0x82, 0xF2, 0xD8, 0x50, 0x0E, 0xC8, 0xF3, 0x38, 0x4C, 0x6F,
            0x9F, 0x31, 0x33, 0x3B, 0x84, 0x80, 0xDC, 0x02, 0x30, 0x28, 0x73, 0x26, 0x67, 0x1A, 0x2A, 0xD5,
            0x81, 0xE7, 0x71, 0xAE, 0xB1, 0xB8, 0x76, 0x20, 0xF5, 0xAF, 0xF0, 0xC5, 0x92, 0xBC, 0x4A, 0xD1,
            0xCE, 0x99, 0x5E, 0x12, 0xFB, 0xF9, 0x0D, 0x97, 0xEC, 0xCC, 0xB3, 0xC9, 0x7F, 0x74, 0x3A, 0x2D,
            0x7D, 0x51, 0x93, 0x70, 0x86, 0x53, 0x94, 0xEF, 0x9C, 0x55, 0xCB, 0x22, 0xE8, 0x4F, 0x44, 0xDB,
            0x00, 0x0B, 0x06, 0x49, 0xB0, 0x40, 0x39, 0xE6, 0x1C, 0x24, 0x45, 0xB2, 0x9D, 0x46, 0x85, 0x8A,
            0xD3, 0xF6, 0x72, 0x2C, 0x0A, 0xA0, 0x90, 0x68, 0xAC, 0x14, 0x3D, 0x42, 0x19, 0xFF, 0xCF, 0x59,
            0xE4, 0xC1, 0x7C, 0x64, 0xF7, 0x5A, 0x9A, 0x56, 0x8C, 0x9E, 0x58, 0x6B, 0xAA, 0x4D, 0x87, 0x29
        };


        private static byte DK_MAPPED = 0X01; //映射类型
        private static byte DK_ENCRYPT = 0X02; //加密类型
        private static byte DK_COMPRESS = 0X04; //压缩类型

        //隐射数据
        public static bool encode(byte[] pInputData, ref byte[] pOuputData, ushort wDataSize,
            bool isFirst = false)
        {
            //Debug.Log("isFirst=>" + isFirst);
            //变量定义
            byte cbCheckCode = 0;

            //映射数据
            int size = 4;
            pOuputData = new byte[pInputData.Length];
            for (int i = size; i < wDataSize; i++)
            {
                cbCheckCode += pInputData[i];
                pOuputData[i] = g_SendByteMap[pInputData[i]];
            }

            byte[] head = new byte[8];
            for (int i = 0; i < 4; i++)
            {
                head[i] = pInputData[i];
            }

            TCP_Info tcp_info = NetUtils.bytesToStruct<TCP_Info>(head);

            byte cbcc = (byte)(~cbCheckCode);
            cbcc += 1;
            tcp_info.cbCheckCode = cbcc;
            tcp_info.wPacketSize = isFirst ? (ushort)(wDataSize + 2) : wDataSize;
            tcp_info.cbDataKind |= DK_MAPPED;

            byte[] newhead = NetUtils.StructToBytes(tcp_info);
            //Debug.Log("newhead length:" + newhead.Length);
            for (int i = 0; i < 4; i++)
            {
                pOuputData[i] = newhead[i];
            }

            //Debug.Log("@@@@mappedBuffer cbDataKind:" + tcp_info.cbDataKind + "  wPacketSize:" + tcp_info.wPacketSize + "  cbCheckCode:" + tcp_info.cbCheckCode);
            return true;
        }

        //隐射数据
        public static bool decode(byte[] pData, ref byte[] pOuputData, uint wDataSize)
        {
            //Debug.LogWarning("datakind is " + pData[0]);
            byte[] head = new byte[4];
            pOuputData = new byte[wDataSize];
            for (int i = 0; i < 4; i++)
            {
                head[i] = pData[i];
                pOuputData[i] = pData[i];
            }

            TCP_Info tcp_info = NetUtils.bytesToStruct<TCP_Info>(head);
            if ((tcp_info.cbDataKind & DK_MAPPED) != 0)
            {
                byte cbCheckCode = tcp_info.cbCheckCode;
                for (int i = 4; i < wDataSize; i++)
                {
                    cbCheckCode += g_RecvByteMap[pData[i]];
                    pOuputData[i] = g_RecvByteMap[pData[i]];
                }

                if (cbCheckCode != 0)
                    return false;
            }

            return true;
        }
    }
}